using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class LiquidFalling : MonoBehaviour
{
	private MeshFilter meshFilter;

	private MeshRenderer meshRender;

	private Vector3[] vertices;

	private int[] triangles;

	private Mesh mesh;

	public int xSize = 12;

	private int ySize = 10;

	private float height;

	public float width;

	private float heightOfStraightSegment;

	private float orginalHeight = 0.04f;

	private float orginalWidth = 0.04f;

	public int resolution = 12;

	private Vector3 start = new Vector3(0f, 0f);

	public Vector3 end = new Vector3(1f, 1f);

	public Vector3 handle1 = new Vector3(0f, 1f);

	public Vector3 handle2 = new Vector3(-1f, 6f);

	[Range(0f, 1f)]
	public float from;

	[Range(0f, 1f)]
	public float to = 1f;

	private void Awake()
	{
		height = orginalHeight;
		width = orginalWidth;
		DefineConnections();
	}

	private void Update()
	{
		GenerateMesh();
	}

	private void DefineConnections()
	{
		meshRender = GetComponent<MeshRenderer>();
		meshFilter = GetComponent<MeshFilter>();
		mesh = meshFilter.mesh;
	}

	private void GenerateCompositeMesh()
	{
		HalfCircleMeshCalculator.CreateHalfCircleDownPart(xSize, width / 2f, out Vector3[] vertList, out int[] triList);
		BezierMeshCalculator.CreateBezierFromTo(start, end, handle1, handle2, out List<Vector3> vertList2, out List<int> triList2, resolution, (float)xSize * width, from, to);
		HalfCircleMeshCalculator.CreateHalfCircleTopPart(xSize, width / 2f, out Vector3[] vertList3, out int[] triList3);
		Vector3 b = new Vector3((float)xSize * width / 2f, 0f);
		Quaternion quaternion = Quaternion.FromToRotation(Vector3.up, vertList2[vertList2.Count - 5] - vertList2[vertList2.Count - 7]);
		for (int i = 0; i < vertList3.Length; i++)
		{
			vertList3[i] += vertList2[vertList2.Count - 1] + b;
			vertList3[i] = RotatePointAroundPivot(vertList3[i], vertList3[0], quaternion.eulerAngles);
		}
		Quaternion quaternion2 = Quaternion.FromToRotation(Vector3.up, vertList2[4] - vertList2[2]);
		for (int j = 0; j < vertList.Length; j++)
		{
			vertList[j] += vertList2[0] - b;
			vertList[j] = RotatePointAroundPivot(vertList[j], vertList[0], quaternion2.eulerAngles);
		}
		vertList2[vertList2.Count - 3] = vertList3[1];
		vertList2[vertList2.Count - 1] = vertList3[vertList3.Length - 1];
		vertList2[vertList2.Count - 2] = vertList3[vertList3.Length - 1];
		vertList2[vertList2.Count - 4] = vertList3[vertList3.Length - 1];
		vertList2[0] = vertList[1];
		MergeTwoMeshes(vertList, triList, vertList2.ToArray(), triList2.ToArray(), out Vector3[] resultVertList, out int[] resultTriList);
		MergeTwoMeshes(resultVertList, resultTriList, vertList3, triList3, out Vector3[] resultVertList2, out int[] resultTriList2);
		vertices = resultVertList2;
		triangles = resultTriList2;
	}

	public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
	{
		Vector3 point2 = point - pivot;
		point2 = Quaternion.Euler(angles) * point2;
		point = point2 + pivot;
		return point;
	}

	protected void MergeTwoMeshes(Vector3[] vertList1, int[] triList1, Vector3[] vertList2, int[] triList2, out Vector3[] resultVertList, out int[] resultTriList)
	{
		resultVertList = new Vector3[vertList1.Length + vertList2.Length];
		resultTriList = new int[triList1.Length + triList2.Length];
		for (int i = 0; i < vertList1.Length; i++)
		{
			resultVertList[i] = vertList1[i];
		}
		for (int j = 0; j < vertList2.Length; j++)
		{
			resultVertList[j + vertList1.Length] = vertList2[j];
		}
		for (int k = 0; k < triList1.Length; k++)
		{
			resultTriList[k] = triList1[k];
		}
		for (int l = 0; l < triList2.Length; l++)
		{
			resultTriList[l + triList1.Length] = triList2[l] + vertList1.Length - 1;
		}
	}

	private void GenerateMesh()
	{
		if (width > 0f)
		{
			GenerateCompositeMesh();
			mesh.vertices = vertices;
			mesh.triangles = triangles;
		}
		else
		{
			mesh.Clear();
		}
		mesh.RecalculateBounds();
		mesh.RecalculateNormals();
	}

	public void SetWidth(float width)
	{
		this.width = width * orginalWidth;
	}

	public void SetPosition(Vector3 start, Vector3 end, Vector3 handle1, Vector3 handle2, float from, float to)
	{
		base.transform.position = start;
		this.end = end - start;
		this.handle1 = handle1;
		this.handle2 = handle2;
		this.from = from;
		this.to = to;
	}

	public void SetMaterial(Material material)
	{
		meshRender.material = material;
	}
}
